03. A Quick Guide to Lint, and Linting Your Project
Before submitting your first project, please take a moment to learn how to lint your files to catch any errors in advance.
Lint, named after the bits of material that needlessly afix themselves to your clothing, was originally a UNIX utility for flagging suspect C code created at Bell Labs in 1978. C is what we'd call a low level language and is very close to machine code. That closeness means a greater likelihood of unintended side effects and bad code due to all the things you must manage on your own. There's even a competition, The International Obfuscated C Code Contest, where people exploit these features to create programs that are intentionally hard to understand.
The original paper, Lint, a C Program Checker, is available free online. Even if you don't know C, there's enough prose to follow the original intent of its creator.
Since the original version, linters have been created for many programming languages.
You might be wondering why we need a linter if we have a compiler. A linter and compiler can overlap in features but have different goals.
The goal of a compiler is to convert your source code into runnable machine instructions. As long as you don't make a syntax error, the compiler will not generally complain. On the other hand, a linter is concerned with the quality of your code. And when I say, code quality, I'm not talking about a style decision like using tabs instead of spaces. Linters check for more hard to debug problems like using a variable before it has been assigned or improperly closing a file and corrupting its contents. That's the type of issue that will compile but is likely NOT desired behavior.
Modern compilers have checks for some of the major breaking errors but their analysis will generally not go as detailed as a linter will.
Running Lint
You can run lint by selecting Inspect Code from the Analyze menu in Android Studio.
From the command-line, you can directly run the project's lint task.
./gradlew lint
When you run Inspect Code from Android Studio, it will blend the warnings and errors flagged by Android's lint task with the code analysis that's built into Android Studio.
The code inspection in Android Studio is great when working on your own project. When working with a collaborator, I'd consider exporting the list to HTML (or using the Gradle task) so they can have an itemized list.
Running Lint In Android Studio and Generating Reports
Running Lint in Android Studio
When is Lint run?
Lint isn't run every time you build. The default build task in Android Studio is assembleDebug which compiles and packages an APK to send to a device or emulator. The build task runs lint and will halt the build process if there are linter errors. Gradle can keep track of which tasks have unchanged source code so if you like to build everything from the command-line, you could execute :
gradle build && gradle installDebug
Gradle will first attempt to build the project, run lint, and if both of those finish without errors, it will proceed with trying to install on a device.
Hopefully you can see how Lint makes your code better. It's good practice to address all the warnings and errors that lint finds. Though all of them won't prevent a successful project submission, they will make you more mindful of the code you are producing and how to avoid potential problems.